<template>
	<view class="loading" style="position: relative; width: 750rpx;height: 100vh;text-align: center;" v-if="isLoading">
		<view class="text position-center" style="width: 750rpx;">
			加载中...
		</view>
	</view>
<!-- 	<view class="answer" @click="login">
		{{answer}}
	</view> -->
	<view class="address address-index" style="width: 750rpx;position: relative;" v-if="!isLoading">
		<view class="address-list" style="width: 750rpx;position: relative;">
			<radio-group @change="radioChange">
				<view @click="addressClick(item)" class="address-list-item flex-row-center" v-for="item in addressList" :key="item.advMembersaddrId">
					<view class="item-left flex-row">
						<view class="left-radio">
							<radio :id="item.advMembersaddrId" :checked="item.recvDefault=='1'" class="position-center" :value="item.advMembersaddrId" color="#ff0000"/>
						</view>
						<label :for="item.advMembersaddrId">
							<view class="left-info">
								<view class="info-top flex-row">
									<view class="info-nickname">
										{{item.recvPerson}}
										{{item.recvTel}}
									</view>
									<view class="info-label" v-if="item.recvDefault=='1'">
										默认
									</view>
								</view>
								<view class="info-bottom hidden-text">
									{{ item.recvAddr }}
								</view>
							</view>
						</label>
					</view>
					<view class="item-edit">
						<view class="edit-icon position-center">
							<uni-icons
								type="compose" 
								size="30"
								@click="editAddress(item.advMembersaddrId)"></uni-icons>
							<uni-icons
								type="trash" 
								size="30"
								color="red"
								@click="removeAddress(item)"></uni-icons>
						</view>
					</view>
				</view>
			</radio-group>
		</view>
		<view class="address-bottom">
			<view class="bottom-button position-center" @click="editAddress()">
				新增收货地址
			</view>
		</view>
	</view>
</template>

<script>
	import { GET_USER_INFO } from '../../../utils/CommonValues.js';
	import request from '../../../utils/request.js';
	export default {
		data() {
			return {
				pageName: '',
				isLoading: true,
				userInfo: {},
				addressList: [],
				advMembersId: '',
				answer: '惦记我',
			}
		},
		onLoad(option) {
			this.pageName = option.pageName; // 上一个页面的名称
			GET_USER_INFO().then(response => {
				this.userInfo = response;
				this.advMembersId = response.advMembersId;
				this.getList();
				console.log(this.userInfo)
			})
		},
		onShow() {
			GET_USER_INFO().then(response => {
				this.userInfo = response;
				this.advMembersId = response.advMembersId;
				this.isLoading = true;
				this.getList();
			})
		},
		methods: {
			login() {
				uni.login({
				    provider: 'weixin',
				    success: function (loginRes) {
				        console.log(loginRes.authResult); // 打印登录凭证
				        // 使用登录凭证获取用户信息
				        uni.getUserInfo({
				            provider: 'weixin',
				            success: function (infoRes) {
				                console.log(infoRes.userInfo); // 打印用户信息
								this.answer = infoRes.userInfo.avatarUrl;
				                console.log('用户昵称为: ' + infoRes.userInfo.nickName); // 打印用户昵称
				                // 在这里，你可以将用户的头像和昵称保存到本地或者发送到后端进行处理
				                // infoRes.userInfo.avatarUrl是用户的头像链接
				                // infoRes.userInfo.nickName是用户的昵称
				            }
				        });
				    }
				});
			},
			// 获取地址信息
			getList() {
				request.GET('/system/membersaddr/list', {
					advMembersId: this.advMembersId
				}).then(response => {
					this.addressList = response.rows;
					this.isLoading = false;
				})
			},
			// 地址被点击
			addressClick(value) {
				console.log(value.advMembersaddrId, "被点击")
				if(this.pageName == 'order') {
					uni.$emit("update-address", value.advMembersaddrId);
					uni.navigateBack();
				}
			},
			// 选择框状态改变
			radioChange(value) {
				// console.log(value.detail.value, "发生改变")
				// if(this.pageName == 'order') {
				// 	uni.$emit("update-address", value.detail.value);
				// 	uni.navigateBack();
				// }
			},
			// 添加[修改]地址
			editAddress(addressId) {
				if(addressId) {
					// 有值表示是修改
					uni.navigateTo({
						url: '/pages/my/editAddress/editAddress?addressId='+addressId
					})
				} else {
					console.log(this.userInfo, '用户id')
					// 没有值表示是添加
					uni.navigateTo({
						url: `/pages/my/editAddress/editAddress?memberId=${this.advMembersId}`
					})
				}
			},
			// 删除地址
			removeAddress(address) {
				uni.showModal({
					title: '提示',
					content: `是否删除地址[${address.recvPerson}]`,
					confirmText: '删除',
					confirmColor: '#ff4500',
					success: (res) => {
						if(res.confirm) {
							// 表示点了确定按钮
							request.DELETE(`/system/membersaddr/${address.advMembersaddrId}`, null).then(response => {
								console.log(response);
								uni.showToast({
									title: '删除成功',
									icon: 'none'
								});
								this.getList();
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
.address-list-item {
	box-sizing: border-box;
	width: 750rpx;
	position: relative;
	height: 60px;
	padding: 20rpx;
	.item-left {
		width: 600rpx;
		height: 100%;
		.left-radio {
			position: relative;
			width: 80rpx;
			height: 100%;
		}
		.left-info {
			width: 520rpx;
			height: 100%;
			.info-top {
				width: 80%;
				height: 50%;
				font-weight: bold;
				.info-label {
					width: 80rpx;
					height: 70%;
					box-sizing: border-box;
					background-color: red;
					font-size: 12px;
					line-height: 21px;
					color: white;
					text-align: center;
				}
			}
			.info-bottom {
				width: 100%;
				height: 50%;
				color: #aaa;
				font-size: 14px;
			}
		}
	}
	.item-edit {
		position: relative;
		width: 120rpx;
		height: 100%;
	}
}
.address-bottom {
	width: 750rpx;
	height: 100px;
	position: relative;
	.bottom-button {
		width: 650rpx;
		height: 40px;
		border-radius: 20px;
		text-align: center;
		line-height: 40px;
		color: white;
		background-color: orangered;
	}
}
</style>
